// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

// 云函数入口函数
exports.main = async (event, context) => {
  let userId = await cloud.database().collection('user')
    .field({
      _id: true
    })
    .skip(event.num)
    .limit(20)
    .get()
  var result = []
  for (var i = 0; i <= userId.data.length; i++) {
    try {
      const res = await cloud.openapi.subscribeMessage.send({
        touser: userId.data[i]._id,
        page: 'pages/article/article?mode=1',
        lang: 'zh_CN',
        data: {
          time1: {
            value: event.time
          },
          thing2: {
            value: '请前往公众号进行投票'
          }
        },
        templateId: 'G0m_yoi3Gz7_osG_wSLVaSGxpz0DNFiE9oT1T1kD8u8',
        miniprogramState: 'formal'
      })
      result = result.concat([res])
    } catch (err) {
      result = result.concat([err])
    }
  }
  return result
}